using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace _1942
{
    /// <summary>
    /// This class is the Animated Sprite for an enemy kamikaze plane
    /// </summary>
    public class Miner : Sprite
    {
        #region Variables

        // Vertical velocity
        protected float Yspeed;

        // Horizontal velocity
        protected int Xspeed;
        
        protected Random random;

	    private bool shouldSpawn = true;

        //Stops the plane from firing nonstop
        private bool canShoot = false;
        
        //Time between shots
        private const int SHOOTTIME = 50;
        private int shootTimer = 0;


        private static List<Rectangle> planeAni = new List<Rectangle>();
        private static List<Rectangle> planeAniR = new List<Rectangle>();
        private static List<Rectangle> explosionAni = new List<Rectangle>();

        //Window size
        private static int windowHeight = -1;
        private static int windowWidth = -1;

        //If between  kill 
        private bool exploding = false;

        //how much hits the plane can take before dying
        private const int HP = 10;
        //current health
        private int health = HP;

        private static Rectangle frame;

        #endregion

        #region Constructors

        public Miner(Game game, ref Texture2D theTexture) :  base(game, ref theTexture)
        {
            //Sets the window size
            if (windowHeight == -1)
            {
                windowHeight = Game.Window.ClientBounds.Height;
                windowWidth = Game.Window.ClientBounds.Width;
            }
            //Sets all the frames for the animations
            #region frames
            if (planeAni.Count == 0)
            {
                frame.X = 4;
                frame.Y = 402;
                frame.Width = 64;
                frame.Height = 60;
                planeAni.Add(frame);
            }
            

            if (planeAniR.Count == 0)
            {
                frame.X = 71;
                frame.Y = 402;
                frame.Width = 64;
                frame.Height = 60;
                planeAniR.Add(frame);
                planeAniR.Add(frame);
                planeAniR.Add(frame);
                planeAniR.Add(frame);
            }
            Frames = planeAni;
            #endregion

            //Sets all the frames for the animations
            #region Explosionframes
            if (explosionAni.Count == 0)
            {
                frame.X = 70;
                frame.Y = 170;
                frame.Width = 32;
                frame.Height = 32;
                explosionAni.Add(frame);

                frame.X = 103;
                explosionAni.Add(frame);

                frame.X = 136;
                explosionAni.Add(frame);

                frame.X = 169;
                explosionAni.Add(frame);

                frame.X = 201;
                explosionAni.Add(frame);

                frame.X = 234;
                explosionAni.Add(frame);
                frame.X = 234;
                explosionAni.Add(frame);
                frame.X = 234;
                explosionAni.Add(frame);
            }
            #endregion

            // Initialize the random number generator and put the plane in it's starting position
            random = new Random(GetHashCode());

            //radomizes when to start shooting
            shootTimer = random.Next(200);
            PutinStartPosition();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Initialize plane Position and Velocity
        /// </summary>
        public void PutinStartPosition()
        {
            explodingSoundplayed = false;
            health = HP;
            exploding = false;
            activeFrame = 0;
            
            if (shouldSpawn)
            {
                if (random.Next(2) == 0)
                {
                    Frames = planeAni;
                    PositionX = -currentFrame.Width + 1;
                    PositionY = (random.Next(windowHeight - currentFrame.Height) + 1) / 4;
                
                    YSpeed = 0.75f;
                    XSpeed = 1;
                }
                else
                {
                    Frames = planeAniR;

                    PositionX = windowWidth + currentFrame.Width - 1;
                    PositionY = (random.Next(windowHeight - currentFrame.Height) + 1) / 4;
                    YSpeed = 0.75f;
                    XSpeed = -1;
                }
            }
            else
            {
                PositionX = -50;
                Visible = false;
            }
        }

        /// <summary>
        /// Update the plane Position
        /// </summary>
        public override void Update(GameTime gameTime)
        {

                // Check if the plane still visible
            if ((Position.Y >= windowHeight + currentFrame.Height) ||
                    (Position.X >= windowWidth + currentFrame.Width) ||
                    (Position.X <= -currentFrame.Width))
                {
                    PutinStartPosition();
                }

                // Move plane
            PositionX = PositionX + Xspeed;
            PositionY = PositionY + Yspeed;

                //Count down shootTimer
                shootTimer--;
                if (shootTimer < 0) canShoot = true;

                if (exploding)
                {
                    if (activeFrame == explosionAni.Count - 1)
                    {
                        PutinStartPosition();
                    }
                }
                base.Update(gameTime);
            
        }

        internal void kill()
        {
            if (!explodingSoundplayed)
            {
                explodingSoundplayed = true;
                audio.EnemyDeath.Play();
            }
            explode();
        }

        private void explode()
        {
            exploding = true;
            Yspeed = 0.75f;
            frameDelay = (long)0.9;
            //Sets all the frames for the animations
            Frames = explosionAni;
            activeFrame = 0;
        }

        public void hit()
        {
            health--;
            if(health < 1) kill();

        }

        #endregion

        #region Properties

        /// Vertical velocity
        /// </summary>
        public float YSpeed
        {
            get { return Yspeed; }
            set
            {
                Yspeed = value;
                frameDelay = (long)(200 - (Yspeed * 5));
            }
        }

        /// <summary>
        /// Horizontal Velocity
        /// </summary>
        public int XSpeed
        {
            get { return Xspeed; }
            set { Xspeed = value; }
        }

        public bool ShouldSpawn
        {
            get { return shouldSpawn; }
            set { shouldSpawn = value; }
        }
        
        public bool Exploding
        {
            get { return exploding; }
        }

        public bool CanShoot
        {
            get { return canShoot; }
            set
            {
                canShoot = value;
                shootTimer = SHOOTTIME;
            }
        }

        #endregion
    }
}